<?php namespace Xcx;

require 'Helper.php';

// 小程序公共类
class Common extends \Phpcmf\Table
{
    public $xcx;

    public function __construct(...$params)
    {
        parent::__construct(...$params);
        \Phpcmf\Service::V()->admin();

        $rt = \Phpcmf\Service::M('xcx', 'xcx')->get_xcx_data(intval($_GET['xid']));
        if (!$rt['code']) {
            $this->_msg(0, $rt['msg']);
        } elseif ($rt['data']['order']['endtime'] < SYS_TIME) {
            $this->_msg(0, '本程序已过期无法使用，请续费');
        }

        $this->xcx = $rt['data'];
        define('XCX_ID', $this->xcx['id']);
        define('XCX_PATH', dirname(APPPATH).'/code/');
        define('XCX_URL', dr_xcx_url('home/index'));
        define('XCX_THEME_PATH', SITE_URL.XCX_DIR.'static/');
        \Phpcmf\Service::V()->assign([
            'xcx' => $this->xcx,
            'meta_title' => $this->xcx['name'],
        ]);
    }

    // 可用的模块
    public function _get_mymodule() {
        $cache = \Phpcmf\Service::L('cache')->get('form-'.SITE_ID);
        $module = [];
        if ($cache) {
            foreach ($cache as $t) {
                if ($t['setting']['dev'] == 'xcx_'.$this->xcx['cid']) {
                    $t['dirname'] = str_replace('xcx_'.$this->xcx['cid'].'_', '', $t['table']);
                    $module[$t['dirname']] = $t;
                }
            }
        }
        return $module;
    }

    // 可用的栏目
    public function _get_category($xid, $mid = '') {
        $cat = [];
        $data = \Phpcmf\Service::M()->table('xcx_category')->where('xid', $xid)->order_by('displayorder ASC,id ASC')->getAll();
        if ($data) {
            // 全部栏目数据
            foreach ($data as $t) {
                if ($mid && !$t['child'] && $t['tid'] == 1 && $t['mid'] != $mid) {
                    continue;
                }
                $cat[$t['id']] = $t;
            }
        }
        return $cat;
    }

}


